//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  File:  menu.c
//
#include <eboot.h>
#include <pagemenu.h>

#include <VFLBuffer.h>
#include <WMRTypes.h>
#include <WMRconfig.h>
#include <FTL.h>
#include <VFL.h>
#include <FIL.h>
#include <WMR_bootpart.h>
#include <ll_nand.h>
//------------------------------------------------------------------------------
//
//  Define:  dimof
//
#ifdef dimof
#undef dimof
#endif
#define dimof(x)                (sizeof(x)/sizeof(x[0]))


extern volatile ROMHDR * pTOC;

//------------------------------------------------------------------------------
static VOID
ShowFlashGeometry(OAL_BLMENU_ITEM *pMenu)
{
	volatile S3C2443_IOPORT_REG *s2443IOP = (S3C2443_IOPORT_REG *)OALPAtoVA(S3C2443_BASE_REG_PA_IOPORT, FALSE);

	SHOW_SETTING_START();

	//GPB5,write protect disable
	s2443IOP->GPBCON = (s2443IOP->GPBCON & ~(0x3<<10)) | (0x1<<10); 
	s2443IOP->GPBDAT = s2443IOP->GPBDAT | (1<<5);
	s2443IOP->GPBUDP = (s2443IOP->GPBUDP & ~(0x3<<10)) | (0x1<<10);	
	
	if (!FMD_Init (NULL, NULL, NULL))
		RETAILMSG(1, (L"BP_ReadLogicalSector : WMR_Init is failed.\r\n"));



	//check TOC
	if (pTOC == (ROMHDR *const) -1)
	{
		while(1); // spin forever!
	}
	OALMSG(OAL_INFO, (L"  RAM Start           : 0x%08x\r\n", pTOC->ulRAMStart));
	OALMSG(OAL_INFO, (L"  RAM Free            : 0x%08x\r\n", pTOC->ulRAMFree));
	OALMSG(OAL_INFO, (L"  RAM End             : 0x%08x\r\n", pTOC->ulRAMEnd));

	//init free memory
	memset((LPVOID)(pTOC->ulRAMFree),0,pTOC->ulRAMEnd-pTOC->ulRAMFree);
	

	SHOW_SETTING_END();


}

//------------------------------------------------------------------------------

static VOID EraseFlash(OAL_BLMENU_ITEM *pMenu)
{
	UINT32 i;
	//for(i=0;i<SUBLKS_TOTAL*2;i++)
	for(i=0;i<8192*2;i++)
	{
		NAND_EraseBlock(i);
		OALMSG(1,(_T("^")));
	}
#if 0	 
	if (FTL_Format() != FTL_SUCCESS)
	{
		OALMSG(OAL_ERROR,(_T("error: FTL_format error\r\n")));
		return;
	}
	OALMSG(OAL_INFO,(_T(" FTL_format ok\r\n")));
#endif	
}


//------------------------------------------------------------------------------

static VOID EraseImage(OAL_BLMENU_ITEM *pMenu)
{
	 
	if (FTL_Format() != FTL_SUCCESS)
	{
		OALMSG(OAL_ERROR,(_T("error: FTL_format error\r\n")));
		return;
	}
	OALMSG(OAL_INFO,(_T(" FTL_format ok\r\n")));

}


//------------------------------------------------------------------------------

static VOID EraseBlock(OAL_BLMENU_ITEM *pMenu)
{
	UINT8 buffer[2048];
	int i;
	for(i=0;i<2048;i++)
		buffer[i]=i;

	//NAND_EraseBlock(2);
	//NAND_WritePage(2, 0, buffer);
	OALLog(L"write block2 over \r\n");
}

//------------------------------------------------------------------------------

static VOID DumpFlash(OAL_BLMENU_ITEM *pMenu)
{
	UINT8 buffer[2048];
	int i,j;
	
	NAND_ReadPage(2, 0, buffer);

	for (i = 0; i < 2048; i += 16)
	{
		OALLog(L"%04x ", i);
		for (j = i; j < i + 16 && j < 2048; j++)
		{
			OALLog(L" %02x", buffer[j]);
		}
		OALLog(L"  ");
		for (j = i; j < i + 16 && j < 2048; j++)
		{
			if (buffer[j] >= ' ' && buffer[j] < 127)
			{
				OALLog(L"%c", buffer[j]);
			}
			else
			{
				OALLog(L".");
			}
		}
		OALLog(L"\r\n");
	}
}

//------------------------------------------------------------------------------

static VOID SetBadBlock(OAL_BLMENU_ITEM *pMenu)
{
	UINT32 data;
	data=INREG32((PULONG)0x70003020);//data abort test
}

//------------------------------------------------------------------------------

static UINT32 OALStringToUINT32(LPCWSTR psz)
{
	UINT32 i = 0;

	// Replace the dots with NULL terminators
	while (psz != NULL && *psz != L'\0')
	{
		if (*psz < L'0' || *psz > L'9')
		{
			break;
		}
		i = i * 10 + (*psz - L'0');
		psz++;
	}

	return i;
}


//------------------------------------------------------------------------------

OAL_BLMENU_ITEM g_menuFlash[] = {
                                    {
                                        L'1', L"Show flash geometry", ShowFlashGeometry,
                                        NULL, NULL, NULL
                                    }, {
                                        L'2', L"Dump flash sector", DumpFlash,
                                        NULL, NULL, NULL
                                    }, {
                                        L'3', L"Erase flash", EraseFlash,
                                        NULL, NULL, NULL
                                    }, {                                    
                                        L'4', L"Erase CE Image", EraseImage,
                                        NULL, NULL, NULL
                                    }, {
                                        L'5', L"Erase block range", EraseBlock,
                                        NULL, NULL, NULL
                                    }, {
                                        L'6', L"Set bad block", SetBadBlock,
                                        NULL, NULL, NULL
                                    },
                                    {
                                        L'0', L"Exit and Continue", NULL,
                                        NULL, NULL, NULL
                                    }, {
                                        0, NULL, NULL,
                                        NULL, NULL, NULL
                                    }
                                };


